package com.aliengen.thingple.thingple.fillingstation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.aliengen.thingple.thingple.AbstractThingpleActivity;
import com.aliengen.thingple.thingple.DialogRFIDReadingFragment;
import com.aliengen.thingple.thingple.R;
import com.aliengen.thingple.thingple.tag.Tag;
import com.aliengen.thingple.thingple.tag.TruckTag;
import com.aliengen.thingple.thingple.tag.exception.InvalidTagException;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class TruckActivity extends AbstractThingpleActivity implements DialogRFIDReadingFragment.ReadingDialogListener {

    DialogRFIDReadingFragment dialog;
    String operation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_truck);
        setOperatorName();

        dialog = new DialogRFIDReadingFragment();
        dialog.init(this);

        Bundle b = getIntent().getExtras();
        if (b != null)
        {
            this.operation = (String) b.get("operation");
        }
    }

    public void onClickReadTruck(View view){
        dialog.startReading();
    }

    public void onClickSkip(View view){
        Intent nextScreen;
        if(operation.compareTo("checkin") == 0) {
            nextScreen = new Intent(getApplicationContext(), CheckInActivity.class);
        }
        else {
            nextScreen = new Intent(getApplicationContext(), CheckOutActivity.class);
        }
        startActivity(nextScreen);
        this.finish();
    }

    public Tag getTagFromBytes(byte[] tag) throws InvalidTagException
    {
        return new TruckTag(tag);
    }

    public void onReadingNew(Tag tag) {
        Intent nextScreen;
        if(operation.compareTo("checkin") == 0) {
             nextScreen = new Intent(getApplicationContext(), CheckInActivity.class);
        }
        else {
            nextScreen = new Intent(getApplicationContext(), CheckOutActivity.class);
        }
        nextScreen.putExtra("truck_tag", tag);
        startActivity(nextScreen);
        this.finish();
    }
}
